package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.DBHelper;

/**
 * Servlet implementation class RegisteEE
 */
@WebServlet("/RegisteEE")
public class RegisteEE extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisteEE() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void handRegisteEE(HttpServletRequest request, HttpServletResponse response){
    	try {
    		
    		HttpSession session=request.getSession(false);
    		if(session.getAttribute("user")==null)
        	{
    			String employeeUsername = request.getParameter("employeeUsername");
    			String name = request.getParameter("name");
    			String password = request.getParameter("psw");
    			String address = request.getParameter("address");
    			String phoneNumber1 = request.getParameter("phoneNumber1");
    			String phoneNumber2 = request.getParameter("phoneNumber2");
    			String phoneNumber = phoneNumber1 + " " + phoneNumber2;
    			String mail = request.getParameter("mail");
    			String introduction = request.getParameter("introduction");
    			String education = request.getParameter("education");
    			Connection con = DBHelper.getConnection();
    			PreparedStatement sql = DBHelper.getprepareStatement(con,"insert into EMPLOYEE values(?,?,?,?,?,?,?,?)");
    			sql.setString(1, employeeUsername);
    			sql.setString(2, name);
    			sql.setString(3, address);
    			sql.setString(4, phoneNumber);
    			sql.setString(5, mail);
    			sql.setString(6, introduction);
    			sql.setString(7, education);
    			sql.setString(8, null);
    			sql.executeUpdate();
    			sql.close();
    			PreparedStatement sql1= DBHelper.getprepareStatement(con,"insert into joblogin values(?,?,?)");
    			sql1.setString(1, employeeUsername);
    			sql1.setString(2, password);
    			sql1.setString(3, "employee");
    			sql1.executeUpdate();
    			sql1.close();
    			con.close();
    			
    		
    	    	session.setAttribute("user", employeeUsername); 	
    			
    			request.getRequestDispatcher("employee.jsp").forward(request,response);
        	}
        	else
        		response.sendRedirect("login.jsp");
    		
			
	    	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		handRegisteEE(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		handRegisteEE(request, response);
	}

}
